package logic

import (
	"context"
	"errors"
	"shuqi-novel/models/mysql"

	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type NovelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewNovelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NovelUpdateLogic {
	return &NovelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说更新
func (l *NovelUpdateLogic) NovelUpdate(in *novelrpc.NovelUpdateRequest) (*novelrpc.NovelUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	err := user.FirstUserByUserId(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("不是更新用户")
	}
	novel := &mysql.Novel{
		Model:     gorm.Model{ID: user.ID},
		Name:      in.Name,
		Title:     in.Title,
		Content:   in.Content,
		Author:    in.Author,
		NovelType: in.NovelType,
	}
	err = novel.NovelUpdate(mysql.DB, novel.ID)
	if err != nil {
		return nil, errors.New("修改失败")
	}
	return &novelrpc.NovelUpdateResponse{
		NovelId: int64(novel.ID),
	}, nil
}
